package com.itsc.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itsc.entities.Repair;
import com.itsc.entities.User;
import com.itsc.service.inter.IRepairService;
import com.itsc.util.GetCurrentTime;
import com.itsc.util.HttpUtil;
import com.itsc.util.page.Page;
import com.itsc.util.page.PageUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class RepairAction implements ModelDriven<Repair>{
	private Repair model = new Repair();
	List<Repair> repairList = new ArrayList<Repair>();
	private IRepairService repairService;
	private List<Integer> check_id;
	private String pageFlag;
	private String repairIdsString;
	private int totalCount = 0;
	
	private String flag;
	private String datemin;// 起始时间
	private String datemax;// 结束时间
	private String searchname;// 查询条件

	/* 分页显示数据 */
	private int curPage = 1;// 当前页
	private int pageSize = 2;// 每页显示条数
	private int countPage;// 总页数
	
	public void setRepairService(IRepairService repairService) {
		this.repairService = repairService;
	}
	
	public List<Integer> getCheck_id() {
		return check_id;
	}

	public void setCheck_id(List<Integer> check_id) {
		this.check_id = check_id;
	}

	public void setPageFlag(String pageFlag) {
		this.pageFlag = pageFlag;
	}

	public void setRepairIdsString(String repairIdsString) {
		this.repairIdsString = repairIdsString;
	}

	public int getTotalCount()
	{
		int totalCount = 0;
		List<Repair> repairList = new ArrayList<Repair>();
		repairList = repairService.selectAll();
		totalCount = repairList.size();
		return totalCount;
	}
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getDatemin() {
		return datemin;
	}

	public void setDatemin(String datemin) {
		this.datemin = datemin;
	}

	public String getDatemax() {
		return datemax;
	}

	public void setDatemax(String datemax) {
		this.datemax = datemax;
	}

	public String getSearchname() {
		return searchname;
	}

	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	@Override
	public Repair getModel() {
		return model;
	}
	
	
	
	public String waitingRepairList()
	{
		int count = repairService.getCountByRepairState("等待处理");
		PageUtils.initParamter(count, pageSize);
		Page page = PageUtils.getPage();
		repairList = repairService.getPageByRepairState("等待处理", page);
		//repairList = repairService.getAllRepairsByRepairState("等待处理");
		ActionContext.getContext().put("repairList", repairList);
		ActionContext.getContext().put("totalCount", getTotalCount());
		ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("等待处理"));
		ActionContext.getContext().put("page", page);
		return "waitingRepairList";
	}
	
	public String repairingList()
	{
		int count = repairService.getCountByRepairState("正在处理");
		PageUtils.initParamter(count, pageSize);
		Page page = PageUtils.getPage();
		repairList = repairService.getPageByRepairState("正在处理", page);
		//repairList = repairService.getAllRepairsByRepairState("正在处理");
		ActionContext.getContext().put("repairList", repairList);
		ActionContext.getContext().put("totalCount", getTotalCount());
		ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("正在处理"));
		ActionContext.getContext().put("page", page);
		return "repairingList";
	}
	
	public String successRepairList()
	{
		repairList = repairService.getAllRepairsByRepairState("处理成功");
		ActionContext.getContext().put("repairList", repairList);
		ActionContext.getContext().put("totalCount", getTotalCount());
		ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("处理成功"));
		return "successRepairList";
	}
	
	public String canNotRepairList()
	{
		int count = repairService.getCountByRepairState("无法解决");
		PageUtils.initParamter(count, pageSize);
		Page page = PageUtils.getPage();
		repairList = repairService.getPageByRepairState("无法解决", page);
		ActionContext.getContext().put("repairList", repairList);
		ActionContext.getContext().put("totalCount", getTotalCount());
		ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("无法解决"));
		ActionContext.getContext().put("page", page);
		return "canNotRepairList";
	}
	
	public String prepareInsert()
	{
		return "prepareInsert";
	}
	
	public String prepareModify()
	{
		Repair repair = repairService.selectById(model.getRepair_id());
		ActionContext.getContext().put("repair", repair);
		return "prepareModify";
	}
	
	public String insert() throws IOException
	{
		ActionContext.getContext().put("totalCount", getTotalCount());
		ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("等待处理"));
		User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
		boolean flag = repairService.selectByString(model.getUser_name());
		if (!flag) {
			if(loginUser.getUser_id() != null && loginUser.getUser_id() != 0)
				model.setUser_id(loginUser.getUser_id());
			model.setRepair_state("等待处理");
			model.setRepair_submit_time(GetCurrentTime.getCurrentTime());
			try
			{
				repairService.save(model);
			}catch(Exception e)
			{
				ActionContext.getContext().put("repair", model);
				ActionContext.getContext().put("error", "新增失败，发生异常");
				return "prepareInsert";
			}
			
			return "repairList";
			
		} else 
		{
			ActionContext.getContext().put("repair", model);
			ActionContext.getContext().put("userPhoneError", "该电话已经存在报障信息中，不用重复报障");
			return "prepareInsert";
		}

	}
	
	public String modify()
	{
		ActionContext.getContext().put("totalCount", getTotalCount());
		Repair repair = repairService.selectById(model.getRepair_id());
		repair.setRepair_state(model.getRepair_state());
		repair.setRemark(model.getRemark());
		try
		{
			repairService.update(repair);
			ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState(model.getRepair_state()));
		}catch(Exception e)
		{
			ActionContext.getContext().put("repair", model);
			ActionContext.getContext().put("error", "修改失败，发生异常");
			return "prepareModify";
		}
		
		return "repairList";
	}
	
	public String acceptRepair() throws IOException
	{
		User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
		Repair repair = repairService.selectById(model.getRepair_id());
		if(repair != null && repair.getRepair_state().toString().equals("等待处理"))
		{
			repair.setRepair_person_name(loginUser.getUser_name());
			repair.setRepair_state("正在处理");
			try
			{
				repairService.update(repair);
			}catch(Exception e)
			{
				ServletActionContext.getResponse().getWriter().print("0");
			}
			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			ServletActionContext.getResponse().getWriter().print("0");
		}
		return null;
	}
	
	public String waitingRepair() throws IOException
	{
		User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
		Repair repair = repairService.selectById(model.getRepair_id());
		if(repair != null 
				&& repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			repair.setRepair_state("等待处理");
			try
			{
				repairService.update(repair);
			}catch(Exception e)
			{
				ServletActionContext.getResponse().getWriter().print("0");
			}
			ServletActionContext.getResponse().getWriter().print("1");
		} else if(!repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			//报障信息不是同一个人处理
			ServletActionContext.getResponse().getWriter().print("2");
		}
		return null;
	}
	
	public String repairing() throws IOException
	{
		User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
		Repair repair = repairService.selectById(model.getRepair_id());
		if(repair != null 
				&& repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			repair.setRepair_state("正在处理");
			try
			{
				repairService.update(repair);
			}catch(Exception e)
			{
				ServletActionContext.getResponse().getWriter().print("0");
			}
			ServletActionContext.getResponse().getWriter().print("1");
		} else if(!repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			//报障信息不是同一个人处理
			ServletActionContext.getResponse().getWriter().print("2");
		}
		return null;
	}
	
	public String successRepair() throws IOException
	{
		User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
		Repair repair = repairService.selectById(model.getRepair_id());
		if(repair != null 
				&& repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			repair.setRepair_state("处理成功");
			try
			{
				repairService.update(repair);
			}catch(Exception e)
			{
				ServletActionContext.getResponse().getWriter().print("0");
			}
			ServletActionContext.getResponse().getWriter().print("1");
		} else if(!repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			//报障信息不是同一个人处理
			ServletActionContext.getResponse().getWriter().print("2");
		}
		return null;
	}
	
	public String canNotRepair() throws IOException
	{
		User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
		Repair repair = repairService.selectById(model.getRepair_id());
		if(repair != null 
				&& repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			repair.setRepair_state("无法解决");
			try
			{
				repairService.update(repair);
			}catch(Exception e)
			{
				ServletActionContext.getResponse().getWriter().print("0");
			}
			ServletActionContext.getResponse().getWriter().print("1");
		} else if(!repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			//不是同一个人
			ServletActionContext.getResponse().getWriter().print("2");
		}
		return null;
	}
	
	public String deleteWaitingRepair() throws IOException
	{
		Repair repair = repairService.selectById(model.getRepair_id());
			try
			{
				repairService.delete(repair);
				
			}catch(Exception e)
			{
				ServletActionContext.getResponse().getWriter().print("0");
			}
			ServletActionContext.getResponse().getWriter().print("1");
		return null;
	}
	
	public String checkMoreDelete() throws IOException
	{
		boolean flag = true;
		String [] repairIdsArray = repairIdsString.split(",");
		if(repairIdsArray != null && repairIdsArray.length > 0)
		{
			User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
			for(int i = 0; i < repairIdsArray.length; i++)
			{
				Repair repair = repairService.selectById(Integer.parseInt(repairIdsArray[i]));
				if(repair != null 
						&& !repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
				{
					flag = false;
				}
			}
		}else
		{
			flag = false;
		}
		if(flag)
		{
			ServletActionContext.getResponse().getWriter().print("1");
		}else
		{
			//不是同一个人
			ServletActionContext.getResponse().getWriter().print("0");
		}
		return null;
	}
	
	public String delete() throws IOException
	{
		User loginUser = (User) ActionContext.getContext().getSession().get("loginUser");
		Repair repair = repairService.selectById(model.getRepair_id());
		if(repair != null 
				&& repair.getRepair_person_name().toString().equals(loginUser.getUser_name().toString()))
		{
			try
			{
				repairService.delete(repair);
				
			}catch(Exception e)
			{
				ServletActionContext.getResponse().getWriter().print("0");
			}
			ServletActionContext.getResponse().getWriter().print("1");
		}else
		{
			//不是同一个人
			ServletActionContext.getResponse().getWriter().print("2");
		}
		return null;
	}
	
	public String moredelete() throws Exception 
	{
		ActionContext.getContext().put("totalCount", getTotalCount());
		for (Integer id : check_id) {
			Repair repair = repairService.selectById(id);
			repairService.delete(repair);
		}
		if(pageFlag != null)
		{
			if(pageFlag.equals("repairing"))
			{
				ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("正在处理"));
				return "repair1";
			}
			if(pageFlag.equals("successRepair"))
			{
				ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("处理成功"));
				return "repair2";
			}
			if(pageFlag.equals("canNotRepair"))
			{
				ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("无法解决"));
				return "repair3";
			}
		}
		ActionContext.getContext().put("stateTotalCount", repairService.getCountByRepairState("等待处理"));
		return "repairList";
	}
	
	public String checkRepair() throws IOException
	{
		boolean flag = repairService.selectByString(model.getUser_phone());
		if (flag) {
			ServletActionContext.getResponse().getWriter().print("0");
		} else {
			ServletActionContext.getResponse().getWriter().print("1");
		}
		return null;
	}
	
	public String search() throws Exception {
		List<Repair> list = null;
		Page page = null;
		if ((searchname == null || searchname.equals("")) && (datemin == null || datemin.equals(""))
				&& (datemax == null || datemax.equals(""))) {
			int count = repairService.getCountByRepairState(flag);
			PageUtils.initParamter(count, pageSize);
			page = PageUtils.getPage();
			if (curPage <= 0) {
				curPage = 1;
			} else if (curPage > page.getTotalPage()) {
				curPage = page.getTotalPage();
			} else {
			}
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = repairService.search(flag, searchname, datemin, datemax, page);
		} else {
			int count = repairService.getCountByRepairState(flag, searchname, datemin, datemax);
			PageUtils.initParamter(count, pageSize);
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = repairService.search(flag, searchname, datemin, datemax, page);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("page", page);
		String js = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
		HttpUtil.rendText(js);
		return null;
	}
}
